<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create new element node</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="domdocument.createdocumentfragment.html">« DOMDocument::createDocumentFragment</a></li>
      <li style="float: right;"><a href="domdocument.createelementns.html">DOMDocument::createElementNS »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.domdocument.html">DOMDocument</a></li>
    <li>Create new element node</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="domdocument.createelement" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMDocument::createElement</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMDocument::createElement</span> &mdash; <span class="dc-title">Create new element node</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domdocument.createelement-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMDocument::createElement</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$localName</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code><span class="initializer"> = &quot;&quot;</span></span>): <span class="type"><span class="type"><a href="class.domelement.html" class="type DOMElement">DOMElement</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   This function creates a new instance of class
   <span class="classname"><a href="class.domelement.html" class="classname">DOMElement</a></span>. 此节点出现在文档中，除非是用诸如
<span class="function"><a href="domnode.appendchild.html" class="function">DOMNode->appendChild()</a></span> 等函数来将其插入。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domdocument.createelement-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">localName</code></dt>

     <dd>

      <p class="para">
       The tag name of the element.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       The value of the element. By default, an empty element will be created.
       The value can also be set later with <a href="class.domnode.html#domnode.props.nodevalue" class="link">DOMElement::$nodeValue</a>.
      </p>
      <p class="para">
       The value is used verbatim except that the &lt; and &gt; entity
       references will escaped. Note that &amp; has to be manually escaped;
       otherwise it is regarded as starting an entity reference. Also &quot; won&#039;t be
       escaped.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domdocument.createelement-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a new instance of class <span class="classname"><a href="class.domelement.html" class="classname">DOMElement</a></span> or <strong><code>false</code></strong>
   if an error occurred.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-domdocument.createelement-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   <dl>
    
     <dt>
<strong><code>DOM_INVALID_CHARACTER_ERR</code></strong></dt>

     <dd>

      <p class="para">
       Raised if <code class="parameter">localName</code> contains an invalid character.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-domdocument.createelement-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5390">
    <p><strong>示例 #1 Creating a new element and inserting it as root</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$dom&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">(</span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'utf-8'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$element&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;the&nbsp;root&nbsp;element!'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;We&nbsp;insert&nbsp;the&nbsp;new&nbsp;element&nbsp;as&nbsp;root&nbsp;(child&nbsp;of&nbsp;the&nbsp;document)<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;test&gt;This is the root element!&lt;/test&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5391">
    <p><strong>示例 #2 Passing text containing an unescaped &amp; as <code class="parameter">value</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dom&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">(</span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'utf-8'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$element&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'me&nbsp;&amp;&nbsp;you'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Warning: DOMDocument::createElement(): unterminated entity reference             you in /in/BjTCg on line 4
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;foo/&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-domdocument.createelement-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    The <code class="parameter">value</code> will <em class="emphasis">not</em> be escaped.
    Use <span class="methodname"><a href="domdocument.createtextnode.html" class="methodname">DOMDocument::createTextNode()</a></span> to create a
    text node with <em class="emphasis">escaping support</em>.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-domdocument.createelement-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domnode.appendchild.html" class="methodname" rel="rdfs-seeAlso">DOMNode::appendChild()</a> - Adds new child at the end of the children</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createattribute.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createAttribute()</a> - Create new attribute</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createattributens.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createAttributeNS()</a> - Create new attribute node with an associated namespace</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createcdatasection.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createCDATASection()</a> - Create new cdata node</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createcomment.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createComment()</a> - Create new comment node</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createdocumentfragment.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createDocumentFragment()</a> - Create new document fragment</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createelementns.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createElementNS()</a> - Create new element node with an associated namespace</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createentityreference.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createEntityReference()</a> - Create new entity reference node</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createprocessinginstruction.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createProcessingInstruction()</a> - Creates new PI node</span></li>
    <li class="member"><span class="methodname"><a href="domdocument.createtextnode.html" class="methodname" rel="rdfs-seeAlso">DOMDocument::createTextNode()</a> - Create new text node</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>